package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.system.domain.Que;
import com.ruoyi.system.domain.QueList;
import com.ruoyi.system.domain.vo.Results;
import com.ruoyi.web.controller.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class questionListController {
    @Autowired
    private com.ruoyi.system.mapper.questionListMapper questionListMapper;
    @Autowired
    private com.ruoyi.system.service.questionListService questionListService;
    //将题存入题库
    @PostMapping("/que_ques_list")
    public R<String> que_ques_list(@RequestBody QueList queList){
        questionListService.save(queList);
        return R.success("ok");
    }
    //删除
    @DeleteMapping("/deleteQueList/{id}")
    public Results deleteQueList(@PathVariable("id") int id){
        questionListService.removeById(id);
        return new Results(400,"删除成功","");
    }
    //查询同一题库的题
    @GetMapping("/selectByQues/{quesId}")
    public R<List<QueList>> selectByQues(@PathVariable("quesId")Integer quesId){
        LambdaQueryWrapper<QueList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(QueList::getQuesId,quesId);
        List<QueList> list = questionListService.list(queryWrapper);
        return  R.success(list);
    }

//    //在题库中添加题目
//    @PostMapping("/insert_que")
//    public R<String> save_Que(@RequestParam int que_id, @RequestParam int ques_id){
//        QueList queList = questionListService.getById(que_id);
//        queList.setQueQuesId(ques_id);
//        return R.success("ok");
//    }
    //选择，填空答题
    @PostMapping("/answer_small")
    public R<Map<List<Que>,Integer>> answer_small(@RequestBody List<Que> list){
        LambdaQueryWrapper<Que> queryWrapper = new LambdaQueryWrapper<>();
        Map<List<Que>, Integer> map = new HashMap<>();
        for (Que que:list){
            //对比答案
            LambdaQueryWrapper<Que> eq = queryWrapper.eq(Que::getQueAnswer, que.getQueAnswer());
            if (eq!=null){
                //答对了
                map.put(list,1);
            }else{
                //答错了
                map.put(list,2);
            }
        }
        return R.success(map);
    }
    //简答题答题
    @PostMapping("/answer_big")
    public R<Map<String, String>> answer_big(@RequestBody List<Que> list){
        LambdaQueryWrapper<Que> queryWrapper = new LambdaQueryWrapper<>();
        Map<String,String> map = new HashMap<>();
        for (Que que:list){
            LambdaQueryWrapper<Que> eq = queryWrapper.eq(Que::getQueAnswer, que.getQueAnswer());
            map.put(que.getQueAnswer(),eq.getEntity().getQueAnswer());
        }
        return R.success(map);
    }


}
